package com.erpanderp.wam.ac.view.reimbursement;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.SwingWorker;

import org.primefaces.component.datatable.DataTable;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.context.RequestContext;

import com.erpanderp.wam.ac.model.entity.reimbursement.BoardingExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.Currency;
import com.erpanderp.wam.ac.model.entity.reimbursement.LodgingExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TransportExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelMiscExpense;
import com.erpanderp.wam.ac.model.entity.travel.TravelExpenseAmount;
import com.erpanderp.wam.ac.model.entity.travel.TravelRequest;
import com.erpanderp.wam.ac.model.service.reimbursement.TravelReimbursementDAO;
import com.erpanderp.wam.ac.model.service.travel.TravelRequestDAO;
import com.erpanderp.wam.adm.model.entity.CustomLookup;
import com.erpanderp.wam.adm.model.entity.LookupValue;
import com.erpanderp.wam.adm.model.service.CustomLookupDAO;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.ValidationUtil;
import com.erpanderp.wam.util.WAMHostManager;

@ManagedBean
@ViewScoped
public class CreateTravelReimbursementBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private HttpSession session = (HttpSession) FacesContext
			.getCurrentInstance().getExternalContext().getSession(true);
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance()
			.getExternalContext().getContext();
	HttpServletRequest request = (HttpServletRequest) FacesContext
			.getCurrentInstance().getExternalContext().getRequest();
	private TimeZone timeZone = TimeZone.getDefault();

	private TravelExpense travelExpense = new TravelExpense();
	private TravelRequest travelRequest = new TravelRequest();

	private TransportExpense transportExpense = new TransportExpense();
	private LodgingExpense lodgingExpense = new LodgingExpense();
	private BoardingExpense boardingExpense = new BoardingExpense();
	private TravelMiscExpense travelMiscExpense = new TravelMiscExpense();
	private EmployeeDAO employeeDAO = new EmployeeDAO();

	private CustomLookupDAO customLookupDAO = new CustomLookupDAO();
	private TravelRequestDAO travelRequestDAO = new TravelRequestDAO();
	private TravelReimbursementDAO travelReimbursementDAO = new TravelReimbursementDAO();

	private Map<String, String> expenseTypesMap = CommonLookup
			.getExpenseTypesMap();
	private String SELECTED_EXPENSE_TYPE = CommonLookup.TRAVEL_EXPENSE_TRANSPORT;
	private Map<String, String> travelModeMap = CommonLookup
			.getTravelModesMap();
	private boolean SHOW_NEW_EXPENSE_FORM;
	private List<TravelExpenseAmount> travelExpenseAmounts = new ArrayList<TravelExpenseAmount>();
	private boolean SHOW_NEW_CURRENCY_FORM;
	private TravelExpenseAmount newTravelExpenseAmount;
	private TravelExpenseAmount markedTravelExpenseAmount;

	private List<LookupValue> lookUpValuesList = new ArrayList<LookupValue>();
	private List<TransportExpense> transportExpensesDetailsList = new ArrayList<TransportExpense>();
	private List<LodgingExpense> lodgingExpenseDetailsList = new ArrayList<LodgingExpense>();
	private List<BoardingExpense> boardingExpenseDetailsList = new ArrayList<BoardingExpense>();
	private List<TravelMiscExpense> travelMiscExpenseDetailsList = new ArrayList<TravelMiscExpense>();
	private List<Currency> Currencies = new ArrayList<Currency>();

	private Map<String, String> currencyMap = new HashMap<String, String>();

	private DataTable transportDetailsTable;
	private DataTable lodgingDetailsTable;
	private DataTable bordingDetailsTable;
	private DataTable travelMiscDetailsTable;

	private WAMUser wamUser;
	private Employee employee;
	private Employee employeeVP;
	private EmailUtil emailUtil;
	private String EMAIL_MESSAGE;

	private String travelCode;
	private String employeeCode;
	private boolean FORM_VALID;
	private boolean TRANSPORT_VALID;
	private boolean LODGING_VALID;
	private boolean BOARDING_VALID;
	private boolean MISC_VALID;

	private Date requestDate;
	private String travelExpenseCode;

	private float convertedTotalINR = 0.0f;

	private float advanceTaken = 0.0f;
	private String advancetakenCurrency;
	private float advanceTakenINR = 0.0f;

	public CreateTravelReimbursementBean() {

		travelCode = request.getParameter("travelCode");
		employeeCode = request.getParameter("employeeCode");

		travelRequest = travelRequestDAO.getTravelRequest(travelCode);
		
		for(TravelExpenseAmount travelExpenseAmount : travelRequest.getTravelExpenseAmounts()){
			if(travelExpenseAmount.getAmountType().equals(CommonLookup.TRAVEL_EXPENSE_TYPE_ADVANCE)){
				travelExpenseAmounts.add(travelExpenseAmount);		
			}
		}
		
		travelExpenseCode = CommonLookup.getAutoGeneratedTravelExpenseFormCode(
				travelCode, employeeCode);

		requestDate = new Date();

		wamUser = (WAMUser) session
				.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
		employee = employeeDAO.getEmployeeByEmployeeId(wamUser.getEmployee()
				.getEmployeeId());
		employeeVP = employeeDAO.getVP();

		lookUpValuesList = customLookupDAO
				.getLookupValuesListByCustomLookupName(CommonLookup.CURRENCY_CUSTOM_LOOKUP);
		for (LookupValue lookUpValue : lookUpValuesList) {
			currencyMap.put(lookUpValue.getLookupName(),
					lookUpValue.getLookupValue());
		}

	}

	public void showNewExpenseForm(ActionEvent e) {
		SHOW_NEW_EXPENSE_FORM = true;
		transportExpense = new TransportExpense();
		lodgingExpense = new LodgingExpense();
		boardingExpense = new BoardingExpense();
		travelMiscExpense = new TravelMiscExpense();
	}

	public void showNewCurrencyPanel(ActionEvent e) {
		setSHOW_NEW_CURRENCY_FORM(true);
		newTravelExpenseAmount = new TravelExpenseAmount();
	}

	public void addAdvanceAmount(ActionEvent e) {
		boolean ADDED = false;
		for (TravelExpenseAmount expenseAmount : travelExpenseAmounts) {
			ADDED = false;
			if (expenseAmount.getCurrency().equals(
					newTravelExpenseAmount.getCurrency())) {
				ADDED = true;
				travelRequest.getTravelExpenseAmounts().remove(expenseAmount);
				expenseAmount.setAmount(expenseAmount.getAmount()
						+ newTravelExpenseAmount.getAmount());
				travelRequest.getTravelExpenseAmounts().add(expenseAmount);			
				break;
			}
		}

		if (!ADDED) {
			newTravelExpenseAmount
					.setAmountType(CommonLookup.TRAVEL_EXPENSE_TYPE_ADVANCE);
			newTravelExpenseAmount.setCreateDate(new Date());
			newTravelExpenseAmount.setCreatedBy(wamUser.getEmployeeCode());
			newTravelExpenseAmount.setStatus(CommonLookup.STATUS_ACTIVE);
			newTravelExpenseAmount.setTravelRequest(travelRequest);
			travelRequest.getTravelExpenseAmounts().add(newTravelExpenseAmount);
			travelExpenseAmounts.clear();
			travelExpenseAmounts.addAll(travelRequest.getTravelExpenseAmounts());
		}else{
			
		}

		SHOW_NEW_CURRENCY_FORM = false;
	}

	public void removeAdvanceAmount(ActionEvent e) {
		markedTravelExpenseAmount = (TravelExpenseAmount) e.getComponent()
				.getAttributes().get("advance");
		travelRequest.getTravelExpenseAmounts().remove(markedTravelExpenseAmount);
		travelExpenseAmounts.clear();
		travelExpenseAmounts.addAll(travelRequest.getTravelExpenseAmounts());
	}

	// SAVE TRAVEL EXPENSE
	public String saveTravelExpenseRequest() {
		validateForm();

		if (FORM_VALID) {
			travelExpense.setTravelRequest(travelRequest);
			travelExpense.setEmployee(employee);
			travelExpense.setRequestDate(new Date());
			travelExpense.setTravelExpenseCode(travelExpenseCode);
			travelExpense.setRecommenderBy(travelRequest.getApprover());
			travelExpense.setApprovedBy(employeeDAO.getVP());
			travelExpense.setRecommenderStatus("Inprocess");
			travelExpense.setRecommenderStatus("Inprocess");
			travelExpense.setApproverStatus("Inprocess");
			travelExpense.setApprovalComments("");
			travelExpense.setAdvanceTaken(advanceTaken);
			travelExpense.setAdvancetakenCurrency(advancetakenCurrency);
			travelExpense.setAdvanceTakenINR(advanceTakenINR);
			travelExpense.getTransportExpenses().addAll(
					transportExpensesDetailsList);// transportExpensesDetailsList
			travelExpense.getLodgingExpenses()
					.addAll(lodgingExpenseDetailsList);
			travelExpense.getBoardingExpenses().addAll(
					boardingExpenseDetailsList);//
			travelExpense.getTravelMiscExpenses().addAll(
					travelMiscExpenseDetailsList);

			travelExpense.setCreateDate(new Date());
			travelExpense.setStatus("ACTIVE");
			travelExpense.setCreatedBy(wamUser.getEmployee().getFirstName()
					+ " " + wamUser.getEmployee().getLastName());

			if (travelReimbursementDAO.createTravelExpense(travelExpense) != null
					&& travelRequestDAO.updateTravelRequest(travelRequest) != null) {
				FacesContext.getCurrentInstance().addMessage(
						null,
						new FacesMessage(FacesMessage.SEVERITY_INFO,
								" You have submitted successfully.", ""));
				System.out.println("AT CALLER METHOD: " + sendMessage()); // *******DONT
																			// REMOVE*******
			}

			transportExpensesDetailsList.clear();
			lodgingExpenseDetailsList.clear();
			boardingExpenseDetailsList.clear();
			travelMiscExpenseDetailsList.clear();
			Currencies.clear();
			return "travelreimbursement";
		} else {
			return null;
		}
	}

	public void expenseTypeChanged(AjaxBehaviorEvent e) {
		System.out.println(((SelectOneMenu) e.getComponent()).getValue());
		System.out.println(SELECTED_EXPENSE_TYPE);
	}

	public void confirmationAction(ActionEvent e) {
		validateForm();

		if (FORM_VALID) {
			RequestContext.getCurrentInstance().execute("confirmation.show();");
		} else {
			RequestContext.getCurrentInstance().execute("confirmation.hide();");
		}
	}

	// ADDIGN CURRENCY TOTALS
	public void totalAmountandCurrencyListAction(String curncy, Float amt) {
		try {
			boolean MATCHED = false;
			for (LookupValue currency : lookUpValuesList) {
				if (curncy.equals(currency.getLookupValue())) {
					MATCHED = true;
					boolean ADDED = false;
					for (Currency cur : Currencies) {
						if (currency.getLookupValue().equals(cur.getCurrency())) {
							ADDED = true;
							// change the value
							cur.setAmount(amt + cur.getAmount());
							System.out.println("UPDATED");
							break;
						}
					}
					if (!ADDED) {
						// create new currentcy and add to the list
						System.out.println("ADDED");
						Currency currencyObj = new Currency();
						currencyObj.setAmount(amt);
						currencyObj.setCurrency(curncy);
						Currencies.add(currencyObj);
					}
					break;
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void subtractTotalAmount(String curncy, Float amt) {
		try {
			for (LookupValue currency : lookUpValuesList) {
				if (curncy.equals(currency.getLookupValue())) {
					for (Currency cur : Currencies) {
						if (currency.getLookupValue().equals(cur.getCurrency())) {
							// change the value
							cur.setAmount(cur.getAmount() - amt);
						}
					}
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	// Add Details List or Table

	public void addAction(ActionEvent e) {
		if (SELECTED_EXPENSE_TYPE.equals(CommonLookup.TRAVEL_EXPENSE_TRANSPORT))
			addTranportExpenseDetailsAction(e);
		if (SELECTED_EXPENSE_TYPE.equals(CommonLookup.TRAVEL_EXPENSE_BOARDING))
			addbordingExpenseDetailsAction(e);
		if (SELECTED_EXPENSE_TYPE.equals(CommonLookup.TRAVEL_EXPENSE_LODGING))
			addLodgingExpenseDetailsAction(e);
		if (SELECTED_EXPENSE_TYPE.equals(CommonLookup.TRAVEL_EXPENSE_MISC))
			addTravelMiscExpenseDetailsAction(e);
		
		SHOW_NEW_EXPENSE_FORM = false;

	}

	public void addTranportExpenseDetailsAction(ActionEvent ae) {
		traportValidateForm();
		if (TRANSPORT_VALID) {
			try {
				transportExpense.setEmployee(employee);
				transportExpense.setCreateDate(new Date());
				transportExpense
						.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				transportExpense.setStatus(CommonLookup.STATUS_ACTIVE);

				transportExpensesDetailsList.add(transportExpense); // ALL
																	// RECORDS
																	// LIST
				totalAmountandCurrencyListAction(
						transportExpense.getTransportCurrency(),
						transportExpense.getTransportAmount());
				transportExpense = new TransportExpense();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void addLodgingExpenseDetailsAction(ActionEvent ae) {
		System.out.println("INSIDE LODGING ACTION========");
		lodgingValidateForm();
		if (LODGING_VALID) {
			try {

				lodgingExpense.setEmployee(employee);
				lodgingExpense.setCreateDate(new Date());
				lodgingExpense
						.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				lodgingExpense.setStatus(CommonLookup.STATUS_ACTIVE);

				lodgingExpenseDetailsList.add(lodgingExpense); // ALL RECORDS
																// LIST
				totalAmountandCurrencyListAction(
						lodgingExpense.getLodgingCurrency(),
						lodgingExpense.getLodgingAmount());
				lodgingExpense = new LodgingExpense();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void addbordingExpenseDetailsAction(ActionEvent ae) {
		System.out.println("INSIDE LODGING ACTION========");
		bordingValidateForm();
		if (BOARDING_VALID) {
			try {
				boardingExpense.setEmployee(employee);
				boardingExpense.setCreateDate(new Date());
				boardingExpense
						.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				boardingExpense.setStatus(CommonLookup.STATUS_ACTIVE);

				boardingExpenseDetailsList.add(boardingExpense); // ALL RECORDS
																	// LIST
				totalAmountandCurrencyListAction(
						boardingExpense.getBoardingCurrency(),
						boardingExpense.getBoardingAmount());
				boardingExpense = new BoardingExpense();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void addTravelMiscExpenseDetailsAction(ActionEvent ae) {
		System.out.println("INSIDE LODGING ACTION========");
		travelMiscValidateForm();
		if (MISC_VALID) {
			try {
				travelMiscExpense.setEmployee(employee);
				travelMiscExpense.setCreateDate(new Date());
				travelMiscExpense
						.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				travelMiscExpense.setStatus(CommonLookup.STATUS_ACTIVE);

				travelMiscExpenseDetailsList.add(travelMiscExpense); // ALL
																		// RECORDS
																		// LIST
				totalAmountandCurrencyListAction(
						travelMiscExpense.getTravelMiscExpensecurrency(),
						travelMiscExpense.getTravelMiscExpenseAmount());

				travelMiscExpense = new TravelMiscExpense();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// SENDING EMAIL
	public String sendMessage() {
		Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
		String domainName = context.getInitParameter("domainName");
		String messageString = "";

		String fromPlace = ""; // DOMESTIC OVERSEAS
		String toPlace = "";
		if (travelRequest.getTravelType().equalsIgnoreCase("DOMESTIC")) {
			fromPlace = travelRequest.getTravelFrom_DO();
			toPlace = travelRequest.getTravelTo_DO();
		} else if (travelRequest.getTravelType().equalsIgnoreCase("OVERSEAS")) {
			fromPlace = travelRequest.getTravelFrom_OS();
			toPlace = travelRequest.getTravelTo_OS();
		} else {
			fromPlace = travelRequest.getTravelFrom_DO();
			toPlace = travelRequest.getTravelTo_OS();
		}

		SimpleDateFormat formate = new SimpleDateFormat("dd-MMM-yyyy");
		String fromDate = formate.format(travelRequest.getPeriodFrom());
		String toDate = formate.format(travelRequest.getPeriodTo());

		emailUtil = new EmailUtil();
		if (hostsMap.get(CommonLookup.DEV_EMAIL) == null) {
			String[] to = { travelExpense.getRecommenderBy().getEmail()};
			emailUtil.setRecieverAddresses(to);
		} else {
			String[] to = { hostsMap.get(CommonLookup.DEV_EMAIL) };
			System.out.println(to);
			emailUtil.setRecieverAddresses(to);
		}

		String requestURL = "ac/reimbursement/approvetravelexpense.html?travelCode="
				+ travelCode
				+ "&expenseCode="
				+ travelExpenseCode
				+ "&employeeCode=" + employeeCode + "&activity=byRecommender";
		String privateLink = "http://" + hostsMap.get(CommonLookup.PRIVATEHOST)
				+ "/" + domainName + "/" + requestURL;
		String publicLink = "http://" + hostsMap.get(CommonLookup.PUBLICHOST)
				+ "/" + domainName + "/" + requestURL;

		messageString = "Dear " + travelExpense.getRecommenderBy().getFirstName()
				+ " " + travelExpense.getRecommenderBy().getLastName() + ""
				+ ",<br /><br />" +

				"<table style='border: 1px solid #d9d9d9;'>"
				+ "<tr> <td>Employee Name</td><td> : </td><td>"
				+ travelRequest.getEmployee().getFirstName() + " "
				+ travelRequest.getEmployee().getLastName() + "</td></tr>"
				+ "<tr> <td>Employee Code</td><td> : </td><td>"
				+ travelRequest.getEmployee().getEmployeeCode() + " </td></tr>"
				+ "<tr> <td>Travel Request Code</td><td> : </td><td>"
				+ travelRequest.getTravelRequestCode() + " </td></tr>"
				+ "<tr> <td>Expense Request Code</td><td> : </td><td>"
				+ travelExpenseCode + " </td></tr>"
				+ "<tr> <td>Contact Number</td><td> : </td><td>"
				+ travelRequest.getEmployee().getTelephone() + " </td></tr>"
				+ "<tr> <td>Travel From</td><td> : </td><td>" + fromPlace
				+ " </td> <td>Travel To</td><td> : </td><td>" + toPlace
				+ " </td></tr>"
				+ "<tr> <td>Travel Start Date</td><td> : </td> <td>" + fromDate
				+ " </td><td>Return Date</td><td> : </td><td>" + toDate
				+ " </td></tr>" + "</table><br/>"
				+ "<a style='color:#FF9933;font-weight: bold;' href=\""
				+ privateLink + "\">"
				+ "Please click here to Recommend/Reject expense form request"
				+ "</a /><br />" + "(Employees who are in Hyderabad office)"
				+ "<br /><br />"
				+ "<a style='color:#FF9933;font-weight: bold;' href=\""
				+ publicLink + "\">"
				+ "Please click here to Recommend/Reject expense form request"
				+ "</a /><br />" + "(out side Hyderabad office)"
				+ "<br /><br />" +

				"Thanks & Regards, <br />" + CommonLookup.WAM_DEVTEAM_SIGNATURE;

		emailUtil.setMessage(messageString);
		emailUtil.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
		emailUtil.setSubject("WAM - " + wamUser.getEmployee().getFirstName()
				+ " " + wamUser.getEmployee().getLastName() + " ("
				+ travelExpenseCode + ") - Travel Expense Request");

		SwingWorker worker = new SwingWorker<String, Object>() {
			@Override
			protected String doInBackground() throws Exception {
				EMAIL_MESSAGE = emailUtil.processAndSendEmail();
				return null;
			}

			@Override
			protected void done() {
				System.out.println("DONE " + EMAIL_MESSAGE);
			}
		};
		worker.execute();
		System.out.println("BEFORE RETURNING: " + EMAIL_MESSAGE);
		return EMAIL_MESSAGE;
	}

	// CHNAGE COLUMNS IN THE TABLE
	/*
	 * public void changeTranportExpenseDetails(AjaxBehaviorEvent e){
	 * System.out.println("INSIDE CHANGE EVENT ::::::");
	 * 
	 * TransportExpense selectedTransport = (TransportExpense)
	 * transportDetailsTable.getRowData();
	 * 
	 * System.out.println("selectedTransportT ::::::"+selectedTransport);
	 * System.out.println("selectedTransport.getTransportCurrency()::::::"+
	 * selectedTransport.getTransportCurrency());
	 * System.out.println("selectedTransport.getTransportAmount() ::::::"
	 * +selectedTransport.getTransportAmount());
	 * 
	 * subtractTotalAmount(selectedTransport.getTransportCurrency(),
	 * selectedTransport.getTransportAmount());
	 * totalAmountandCurrencyListAction(
	 * selectedTransport.getTransportCurrency(),
	 * selectedTransport.getTransportAmount());
	 * 
	 * System.out.println("==================SIZE ================="+
	 * transportExpensesDetailsList.size()); for(TransportExpense
	 * transportExpense : transportExpensesDetailsList){
	 * subtractTotalAmount(transportExpense
	 * .getTransportCurrency(),transportExpense.getTransportAmount());
	 * totalAmountandCurrencyListAction
	 * (transportExpense.getTransportCurrency(),transportExpense
	 * .getTransportAmount());
	 * System.out.println("==============>>>>>>>>>>>>>>>"
	 * +transportExpense.getTransportAmount()); } }
	 */

	// DELETE RECORDS FROM LIST(s)
	public void removeTransportDetails(ActionEvent ae) {
		TransportExpense selectedTransport = (TransportExpense) transportDetailsTable
				.getRowData();
		subtractTotalAmount(selectedTransport.getTransportCurrency(),
				selectedTransport.getTransportAmount());
		transportExpensesDetailsList.remove(selectedTransport);
	}

	public void removeLodgingDetails(ActionEvent ae) {
		LodgingExpense selectedlodging = (LodgingExpense) lodgingDetailsTable
				.getRowData();
		subtractTotalAmount(selectedlodging.getLodgingCurrency(),
				selectedlodging.getLodgingAmount());
		lodgingExpenseDetailsList.remove(selectedlodging);
	}

	public void removeBordingDetails(ActionEvent ae) {
		BoardingExpense selectedbording = (BoardingExpense) bordingDetailsTable
				.getRowData();
		subtractTotalAmount(selectedbording.getBoardingCurrency(),
				selectedbording.getBoardingAmount());
		boardingExpenseDetailsList.remove(selectedbording);
	}

	public void removeTravelMiscDetails(ActionEvent ae) {
		TravelMiscExpense selectedTravelMisc = (TravelMiscExpense) travelMiscDetailsTable
				.getRowData();
		subtractTotalAmount(selectedTravelMisc.getTravelMiscExpensecurrency(),
				selectedTravelMisc.getTravelMiscExpenseAmount());
		travelMiscExpenseDetailsList.remove(selectedTravelMisc);
	}

	// REST BUTTON ACTIONS
	public void discardNewExpenseDetails(ActionEvent ae) {
		SHOW_NEW_EXPENSE_FORM = false;
		transportExpense.setTransportDate(null);
		transportExpense.setTransportFrom("");
		transportExpense.setTransportTo("");
		transportExpense.setTransportAmount(0.0f);
	}

	public void resetLodgingExpenseDetailsAction(ActionEvent ae) {
		lodgingExpense.setLodgingDate(null);
		lodgingExpense.setLodgingHotel("");
		lodgingExpense.setLodgingPlace("");
		lodgingExpense.setLodgingAmount(0.0f);
	}

	public void resetbordingExpenseDetailsAction(ActionEvent ae) {
		boardingExpense.setBoardingDate(null);
		boardingExpense.setBoardingHotel("");
		boardingExpense.setBoardingPlace("");
		boardingExpense.setBoardingAmount(0.0f);
	}

	public void resetTravelMiscExpenseDetailsAction(ActionEvent ae) {
		travelMiscExpense.setExpenseDate(null);
		travelMiscExpense.setBillDesc("");
		travelMiscExpense.setBillNo("");
		travelMiscExpense.setTravelMiscExpenseAmount(0.0f);
	}

	// VALIDATIONS
	private void traportValidateForm() {
		TRANSPORT_VALID = true;
		if (ValidationUtil
				.isEmpty(
						(transportExpense.getTransportDate() != null) ? transportExpense
								.getTransportDate().toString() : null, " Date",
						null, FacesContext.getCurrentInstance()) != null) {
			TRANSPORT_VALID = false;
		}
		if (ValidationUtil.isEmpty(transportExpense.getTransportFrom(),
				"Travel From", null, FacesContext.getCurrentInstance()) != null) {
			TRANSPORT_VALID = false;
		}
		if (ValidationUtil.isEmpty(transportExpense.getTransportTo(),
				"Travel To", null, FacesContext.getCurrentInstance()) != null) {
			TRANSPORT_VALID = false;
		}
		if (transportExpense.getTransportAmount() == 0) {
			TRANSPORT_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Expense amount should not zero.", ""));
		}
		if (transportExpense.getTransportAmount() < 0) {
			TRANSPORT_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Expense amount should be grater than zero.", ""));
			transportExpense.setTransportAmount(0.0f);
		}
	}

	private void lodgingValidateForm() {
		LODGING_VALID = true;
		if (ValidationUtil.isEmpty(
				(lodgingExpense.getLodgingDate() != null) ? lodgingExpense
						.getLodgingDate().toString() : null, " Date", null,
				FacesContext.getCurrentInstance()) != null) {
			LODGING_VALID = false;
		}
		if (ValidationUtil.isEmpty(lodgingExpense.getLodgingHotel(),
				"Hotel Name", null, FacesContext.getCurrentInstance()) != null) {
			LODGING_VALID = false;
		}
		if (ValidationUtil.isEmpty(lodgingExpense.getLodgingPlace(), "Place",
				null, FacesContext.getCurrentInstance()) != null) {
			LODGING_VALID = false;
		}
		if (lodgingExpense.getLodgingAmount() == 0) {
			LODGING_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Expense amount should not zero.", ""));
		}
		if (lodgingExpense.getLodgingAmount() < 0) {
			LODGING_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Expense amount should be grater than zero.", ""));
			lodgingExpense.setLodgingAmount(0.0f);
		}
	}

	private void bordingValidateForm() {
		BOARDING_VALID = true;
		if (ValidationUtil.isEmpty(
				(boardingExpense.getBoardingDate() != null) ? boardingExpense
						.getBoardingDate().toString() : null, " Date", null,
				FacesContext.getCurrentInstance()) != null) {
			BOARDING_VALID = false;
		}
		if (ValidationUtil.isEmpty(boardingExpense.getBoardingHotel(),
				"Hotel Name", null, FacesContext.getCurrentInstance()) != null) {
			BOARDING_VALID = false;
		}
		if (ValidationUtil.isEmpty(boardingExpense.getBoardingPlace(), "Place",
				null, FacesContext.getCurrentInstance()) != null) {
			BOARDING_VALID = false;
		}
		if (boardingExpense.getBoardingAmount() == 0) {
			BOARDING_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Expense amount should not zero.", ""));
		}
		if (boardingExpense.getBoardingAmount() < 0) {
			BOARDING_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Expense amount should be grater than zero.", ""));
			boardingExpense.setBoardingAmount(0.0f);
		}
	}

	private void travelMiscValidateForm() {
		MISC_VALID = true;
		if (ValidationUtil
				.isEmpty(
						(travelMiscExpense.getExpenseDate() != null) ? travelMiscExpense
								.getExpenseDate().toString() : null, " Date",
						null, FacesContext.getCurrentInstance()) != null) {
			MISC_VALID = false;
		}
		if (ValidationUtil.isEmpty(travelMiscExpense.getBillDesc(),
				"Hotel Name", null, FacesContext.getCurrentInstance()) != null) {
			MISC_VALID = false;
		}
		if (travelMiscExpense.getTravelMiscExpenseAmount() == 0) {
			MISC_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Expense amount should not zero.", ""));
		}
		if (travelMiscExpense.getTravelMiscExpenseAmount() < 0) {
			MISC_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Expense amount should be grater than zero.", ""));
			travelMiscExpense.setTravelMiscExpenseAmount(0.0f);
		}
	}

	// Final validation
	private void validateForm() {
		FORM_VALID = true;
		int count = Currencies.size();
		System.out.println("=========count=============" + count);
		float amount = 0;

		for (Currency currency : Currencies) {
			amount = currency.getAmount();
			amount++;
		}
		System.out.println("=========amount===============" + amount);

		if (count == 0) {
			FORM_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"You have not added any expense. Please add expense to proceed.", ""));
		}
		if (amount <= 0) {
			FORM_VALID = false;
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"You can not claim with zero amount.",
							""));
		}

	}

	// Generate Setters and Getters
	public TravelExpense getTravelExpense() {
		return travelExpense;
	}

	public void setTravelExpense(TravelExpense travelExpense) {
		this.travelExpense = travelExpense;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public CustomLookupDAO getCustomLookupDAO() {
		return customLookupDAO;
	}

	public void setCustomLookupDAO(CustomLookupDAO customLookupDAO) {
		this.customLookupDAO = customLookupDAO;
	}

	public String getTravelCode() {
		return travelCode;
	}

	public void setTravelCode(String travelCode) {
		this.travelCode = travelCode;
	}

	public String getEmployeeCode() {
		return employeeCode;
	}

	public void setEmployeeCode(String employeeCode) {
		this.employeeCode = employeeCode;
	}

	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public Date getRequestDate() {
		return requestDate;
	}

	public void setRequestDate(Date requestDate) {
		this.requestDate = requestDate;
	}

	public String getTravelExpenseCode() {
		return travelExpenseCode;
	}

	public void setTravelExpenseCode(String travelExpenseCode) {
		this.travelExpenseCode = travelExpenseCode;
	}

	public TravelRequest getTravelRequest() {
		return travelRequest;
	}

	public void setTravelRequest(TravelRequest travelRequest) {
		this.travelRequest = travelRequest;
	}

	public TravelRequestDAO getTravelRequestDAO() {
		return travelRequestDAO;
	}

	public void setTravelRequestDAO(TravelRequestDAO travelRequestDAO) {
		this.travelRequestDAO = travelRequestDAO;
	}

	public Map<String, String> getTravelModeMap() {
		return travelModeMap;
	}

	public void setTravelModeMap(Map<String, String> travelModeMap) {
		this.travelModeMap = travelModeMap;
	}

	public TransportExpense getTransportExpense() {
		return transportExpense;
	}

	public void setTransportExpense(TransportExpense transportExpense) {
		this.transportExpense = transportExpense;
	}

	public float getConvertedTotalINR() {
		return convertedTotalINR;
	}

	public void setConvertedTotalINR(float convertedTotalINR) {
		this.convertedTotalINR = convertedTotalINR;
	}

	public List<LookupValue> getLookUpValuesList() {
		return lookUpValuesList;
	}

	public void setLookUpValuesList(List<LookupValue> lookUpValuesList) {
		this.lookUpValuesList = lookUpValuesList;
	}

	public Map<String, String> getCurrencyMap() {
		return currencyMap;
	}

	public void setCurrencyMap(Map<String, String> currencyMap) {
		this.currencyMap = currencyMap;
	}

	public List<TransportExpense> getTransportExpensesDetailsList() {
		return transportExpensesDetailsList;
	}

	public void setTransportExpensesDetailsList(
			List<TransportExpense> transportExpensesDetailsList) {
		this.transportExpensesDetailsList = transportExpensesDetailsList;
	}

	public boolean isTRANSPORT_VALID() {
		return TRANSPORT_VALID;
	}

	public void setTRANSPORT_VALID(boolean tRANSPORT_VALID) {
		TRANSPORT_VALID = tRANSPORT_VALID;
	}

	public DataTable getTransportDetailsTable() {
		return transportDetailsTable;
	}

	public void setTransportDetailsTable(DataTable transportDetailsTable) {
		this.transportDetailsTable = transportDetailsTable;
	}

	public List<LodgingExpense> getLodgingExpenseDetailsList() {
		return lodgingExpenseDetailsList;
	}

	public void setLodgingExpenseDetailsList(
			List<LodgingExpense> lodgingExpenseDetailsList) {
		this.lodgingExpenseDetailsList = lodgingExpenseDetailsList;
	}

	public LodgingExpense getLodgingExpense() {
		return lodgingExpense;
	}

	public void setLodgingExpense(LodgingExpense lodgingExpense) {
		this.lodgingExpense = lodgingExpense;
	}

	public BoardingExpense getBoardingExpense() {
		return boardingExpense;
	}

	public void setBoardingExpense(BoardingExpense boardingExpense) {
		this.boardingExpense = boardingExpense;
	}

	public TravelMiscExpense getTravelMiscExpense() {
		return travelMiscExpense;
	}

	public void setTravelMiscExpense(TravelMiscExpense travelMiscExpense) {
		this.travelMiscExpense = travelMiscExpense;
	}

	public List<BoardingExpense> getBoardingExpenseDetailsList() {
		return boardingExpenseDetailsList;
	}

	public void setBoardingExpenseDetailsList(
			List<BoardingExpense> boardingExpenseDetailsList) {
		this.boardingExpenseDetailsList = boardingExpenseDetailsList;
	}

	public List<TravelMiscExpense> getTravelMiscExpenseDetailsList() {
		return travelMiscExpenseDetailsList;
	}

	public void setTravelMiscExpenseDetailsList(
			List<TravelMiscExpense> travelMiscExpenseDetailsList) {
		this.travelMiscExpenseDetailsList = travelMiscExpenseDetailsList;
	}

	public boolean isLODGING_VALID() {
		return LODGING_VALID;
	}

	public void setLODGING_VALID(boolean lODGING_VALID) {
		LODGING_VALID = lODGING_VALID;
	}

	public boolean isBOARDING_VALID() {
		return BOARDING_VALID;
	}

	public void setBOARDING_VALID(boolean bOARDING_VALID) {
		BOARDING_VALID = bOARDING_VALID;
	}

	public boolean isMISC_VALID() {
		return MISC_VALID;
	}

	public void setMISC_VALID(boolean mISC_VALID) {
		MISC_VALID = mISC_VALID;
	}

	public DataTable getLodgingDetailsTable() {
		return lodgingDetailsTable;
	}

	public void setLodgingDetailsTable(DataTable lodgingDetailsTable) {
		this.lodgingDetailsTable = lodgingDetailsTable;
	}

	public DataTable getBordingDetailsTable() {
		return bordingDetailsTable;
	}

	public void setBordingDetailsTable(DataTable bordingDetailsTable) {
		this.bordingDetailsTable = bordingDetailsTable;
	}

	public DataTable getTravelMiscDetailsTable() {
		return travelMiscDetailsTable;
	}

	public void setTravelMiscDetailsTable(DataTable travelMiscDetailsTable) {
		this.travelMiscDetailsTable = travelMiscDetailsTable;
	}

	/*
	 * public Currency getCurrency() { return currency; }
	 * 
	 * public void setCurrency(Currency currency) { this.currency = currency; }
	 */

	public List<Currency> getCurrencies() {
		return Currencies;
	}

	public void setCurrencies(List<Currency> currencies) {
		Currencies = currencies;
	}

	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public TravelReimbursementDAO getTravelReimbursementDAO() {
		return travelReimbursementDAO;
	}

	public void setTravelReimbursementDAO(
			TravelReimbursementDAO travelReimbursementDAO) {
		this.travelReimbursementDAO = travelReimbursementDAO;
	}

	public EmailUtil getEmailUtil() {
		return emailUtil;
	}

	public void setEmailUtil(EmailUtil emailUtil) {
		this.emailUtil = emailUtil;
	}

	public String getEMAIL_MESSAGE() {
		return EMAIL_MESSAGE;
	}

	public void setEMAIL_MESSAGE(String eMAIL_MESSAGE) {
		EMAIL_MESSAGE = eMAIL_MESSAGE;
	}

	public float getAdvanceTaken() {
		return advanceTaken;
	}

	public void setAdvanceTaken(float advanceTaken) {
		this.advanceTaken = advanceTaken;
	}

	public String getAdvancetakenCurrency() {
		return advancetakenCurrency;
	}

	public void setAdvancetakenCurrency(String advancetakenCurrency) {
		this.advancetakenCurrency = advancetakenCurrency;
	}

	public float getAdvanceTakenINR() {
		return advanceTakenINR;
	}

	public void setAdvanceTakenINR(float advanceTakenINR) {
		this.advanceTakenINR = advanceTakenINR;
	}

	public Employee getEmployeeVP() {
		return employeeVP;
	}

	public void setEmployeeVP(Employee employeeVP) {
		this.employeeVP = employeeVP;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	public Map<String, String> getExpenseTypesMap() {
		return expenseTypesMap;
	}

	public void setExpenseTypesMap(Map<String, String> expenseTypesMap) {
		this.expenseTypesMap = expenseTypesMap;
	}

	public String getSELECTED_EXPENSE_TYPE() {
		return SELECTED_EXPENSE_TYPE;
	}

	public void setSELECTED_EXPENSE_TYPE(String sELECTED_EXPENSE_TYPE) {
		SELECTED_EXPENSE_TYPE = sELECTED_EXPENSE_TYPE;
	}

	public boolean isSHOW_NEW_EXPENSE_FORM() {
		return SHOW_NEW_EXPENSE_FORM;
	}

	public void setSHOW_NEW_EXPENSE_FORM(boolean sHOW_NEW_EXPENSE_FORM) {
		SHOW_NEW_EXPENSE_FORM = sHOW_NEW_EXPENSE_FORM;
	}

	public List<TravelExpenseAmount> getTravelExpenseAmounts() {
		return travelExpenseAmounts;
	}

	public void setTravelExpenseAmounts(
			List<TravelExpenseAmount> travelExpenseAmounts) {
		this.travelExpenseAmounts = travelExpenseAmounts;
	}

	public boolean isSHOW_NEW_CURRENCY_FORM() {
		return SHOW_NEW_CURRENCY_FORM;
	}

	public void setSHOW_NEW_CURRENCY_FORM(boolean sHOW_NEW_CURRENCY_FORM) {
		SHOW_NEW_CURRENCY_FORM = sHOW_NEW_CURRENCY_FORM;
	}

	public TravelExpenseAmount getNewTravelExpenseAmount() {
		return newTravelExpenseAmount;
	}

	public void setNewTravelExpenseAmount(
			TravelExpenseAmount newTravelExpenseAmount) {
		this.newTravelExpenseAmount = newTravelExpenseAmount;
	}

	public TravelExpenseAmount getMarkedTravelExpenseAmount() {
		return markedTravelExpenseAmount;
	}

	public void setMarkedTravelExpenseAmount(
			TravelExpenseAmount markedTravelExpenseAmount) {
		this.markedTravelExpenseAmount = markedTravelExpenseAmount;
	}

}
